package com.linjiahao.userCenter.controller;

import com.linjiahao.servicebase.common.ResultMap;
import com.linjiahao.servicebase.exception.ErrorMessage;
import com.linjiahao.servicebase.exception.WebChatException;
import com.linjiahao.servicebase.util.JWTUtil;
import com.linjiahao.userCenter.service.ChatFriendService;
import com.linjiahao.userCenter.vo.AddFriendVO;
import com.linjiahao.userCenter.vo.AddOrRejectVO;
import lombok.extern.slf4j.Slf4j;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName ChatFriendController
 * @Description: 用户好友服务控制层
 * @Author linjiahao
 * @Date 6/1/2022
 **/
@RestController
@Slf4j
@RequestMapping("/userCenter/friend")
public class ChatFriendController {

    @Autowired
    private ChatFriendService chatFriendService;

    @PostMapping("/auth/applyFriend")
    public ResultMap applyFriend(@RequestBody AddFriendVO addFriendVO, HttpServletRequest request){
        if(addFriendVO.getFriendId() == null){
            throw new WebChatException(ErrorMessage.ADD_FRIEND_NOT_FOUND.getErrorCode(),ErrorMessage.ADD_FRIEND_NOT_FOUND.getMessage());
        }
        return chatFriendService.applyFriend(addFriendVO, JWTUtil.getUserId(request));
    }

    @GetMapping("/auth/getFriendList")
    public ResultMap getFriendList(HttpServletRequest request) throws BadHanyuPinyinOutputFormatCombination {
        return chatFriendService.getFriendList(JWTUtil.getUserId(request));
    }

    @GetMapping("/auth/getApplyFriendList")
    public ResultMap getApplyFriendList(HttpServletRequest request){
        return chatFriendService.getApplyFriendList(JWTUtil.getUserId(request));
    }

    @PostMapping("/auth/addOrRejectFriend")
    public ResultMap addOrRejectFriend(@RequestBody AddOrRejectVO addOrRejectVO, HttpServletRequest request){
        if(addOrRejectVO == null){
            throw new WebChatException(ErrorMessage.PARAMETER_CANNOT_EMPTY.getErrorCode(),ErrorMessage.PARAMETER_CANNOT_EMPTY.getMessage());
        }
        return chatFriendService.addOrRejectFriend(addOrRejectVO,JWTUtil.getUserId(request));
    }

    @PostMapping("/auth/searchMyFriend")
    public ResultMap searchMyFriend(@RequestParam String keyWord, HttpServletRequest request) throws BadHanyuPinyinOutputFormatCombination {
        if(StringUtils.isEmpty(keyWord)){
            throw new WebChatException(ErrorMessage.PARAMETER_CANNOT_EMPTY.getErrorCode(),ErrorMessage.PARAMETER_CANNOT_EMPTY.getMessage());
        }
        return chatFriendService.searchMyFriend(keyWord,JWTUtil.getUserId(request));
    }

}
